package top.zywork.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.constant.CommonConstant;
import top.zywork.dos.SchoolParcelDO;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.SchoolParcelDTO;
import top.zywork.dto.SchoolPlaceDTO;
import top.zywork.query.SchoolParcelQuery;
import top.zywork.service.SchoolParcelService;
import top.zywork.service.SchoolService;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.SchoolParcelVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  包裹大小前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/school-parcel")
public class SchoolParcelController {

    @Autowired
    private SchoolParcelService schoolParcelService;

    @Autowired
    private SchoolService schoolService;

    /**
     * 通过学校ID查询对应的包裹类型
     * @param schoolId 学校ID
     * @return
     */
    @PostMapping("any/bySchoolId/{id}")
    public ResponseStatusVO bySchoolId(@PathVariable("id") Long schoolId){
        List list = schoolParcelService.list(new QueryWrapper<SchoolParcelDO>()
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE)
                .eq("school_id", schoolId));
        return ResponseStatusVO.ok("查询成功", BeanUtils.copyList(list, SchoolParcelVO.class));
    }

    @PostMapping("admin/save")
    @SysLog(description = "后台保存SchoolParcelVO")
    public ResponseStatusVO save(@RequestBody @Validated SchoolParcelVO schoolParcelVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        SchoolParcelDO copy = BeanUtils.copy(schoolParcelVO, SchoolParcelDO.class);
        copy.setIsActive(CommonConstant.IS_ACTIVE_TRUE);
        copy.setCreateTime(new Date());
        copy.setUpdateTime(new Date());
        schoolParcelService.save(copy);
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存SchoolParcelVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<SchoolParcelVO> schoolParcelVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        BeanUtils.copyListObj(schoolParcelVOList, SchoolParcelDO.class);

        List<SchoolParcelDO> list;
        list = schoolParcelVOList.stream().map(e->BeanUtils.copy(e,SchoolParcelDO.class)).collect(Collectors.toList());

        schoolParcelService.saveBatch(list);
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除SchoolParcelVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        schoolParcelService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除SchoolParcelVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        List<String> id = new ArrayList<>();
        Collections.addAll(id, ids);
        schoolParcelService.removeByIds(id);
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新SchoolParcelVO")
    public ResponseStatusVO update(@RequestBody @Validated SchoolParcelVO schoolParcelVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        SchoolParcelDO copy = BeanUtils.copy(schoolParcelVO, SchoolParcelDO.class);
        copy.setUpdateTime(new Date());
        boolean result = schoolParcelService.updateById(copy);
        if (result) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新SchoolParcelVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<SchoolParcelVO> schoolParcelVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        List<SchoolParcelDO> list;
        list = schoolParcelVOList.stream().map(e->BeanUtils.copy(e,SchoolParcelDO.class)).collect(Collectors.toList());
        schoolParcelService.updateBatchById(list);
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结SchoolParcelVO")
    public ResponseStatusVO active(@RequestBody SchoolParcelVO schoolParcelVO) {
        schoolParcelService.update(new UpdateWrapper<SchoolParcelDO>().eq("id",schoolParcelVO.getId())
                .set("is_active",schoolParcelVO.getIsActive()));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结SchoolParcelVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<SchoolParcelVO> schoolParcelVOList) {
        List<SchoolParcelDO> list;
        list = schoolParcelVOList.stream().map(e->BeanUtils.copy(e,SchoolParcelDO.class)).collect(Collectors.toList());
        schoolParcelService.updateBatchById(list);
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        SchoolParcelVO schoolParcelVO = new SchoolParcelVO();
        Object obj = schoolParcelService.getById(id);
        if (obj != null) {
            schoolParcelVO = BeanUtils.copy(obj, SchoolParcelVO.class);
        }
        return ResponseStatusVO.ok("查询成功", schoolParcelVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        IPage page = schoolParcelService.page(null);
        PagerDTO pagerDTO = PagerDTO.builder().total(page.getTotal()).rows(page.getRecords()).build();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolParcelDTO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody SchoolParcelQuery schoolParcelQuery) {
        PagerDTO pagerDTO = schoolParcelService.listAllByCondition(schoolParcelQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolParcelVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody SchoolParcelQuery schoolParcelQuery) {
        PagerDTO pagerDTO = schoolParcelService.listPageByCondition(schoolParcelQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);

        List<Object> list;

        list = pagerDTO.getRows().stream().map(e->{
            SchoolParcelDO schoolPlaceDTO = (SchoolParcelDO)e;
            SchoolParcelVO schoolParcelVO = new SchoolParcelVO();
            BeanUtils.copy(schoolPlaceDTO,schoolParcelVO);
            schoolParcelVO.setSchoolName(schoolService.getById(schoolPlaceDTO.getSchoolId()).getSchool());
            return schoolParcelVO;
        }).collect(Collectors.toList());
        pagerVO.setRows(list);
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

}
